<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class OrganizationTable extends AbstractTable
{

    protected $table = 'hy_organization';

    protected $id = 'org_id';

    public function find($query = array())
    {
        // 查询条件 城市 商户类型
        $select = new Select(array(
            'o' => $this->getTable()
        ));
//         $select->columns(array(
//             'catalog_id',
//             'org_id',
//             'org_name',
//             'org_advice',
//             'org_level',
//             'org_logo',
//         ));
        $select->join(array(
            'c' => 'hy_catalog'
        ), 'c.catalog_id=o.catalog_id', array(
            'catalog_name'
        ), $select::JOIN_LEFT);
        $select->join(array(
            'y' => 'hy_city'
        ), 'y.city_id=c.city_id', array(
            'city_name'
        ), $select::JOIN_LEFT);
        
        if ($query['catalog_id']) {
            $select->where(array(
                'c.catalog_id' => $query['catalog_id']
            ));
        }
        
        if ($query['city_id']) {
            $select->where(array(
                'y.city_id' => $query['city_id']
            ));
        }
        
        if (! empty($query['org_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('o.org_name', '%'.$query['org_name'] . "%");
            };
        
            $select->where($spec);
        }
        
        if ($query['org_is_direct_sale']) {
            $select->where(array(
                'o.org_is_direct_sale' => $query['org_is_direct_sale']
            ));
        }
        
        if ($query['admin_org_id']) {
            $select->where(array(
                'o.org_id' => $query['admin_org_id']
            ));
        }
        
        $select->order('o.org_id DESC');

        return $this->fetchPaginate($select, $query);

    }

    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }
    
    /**
     * 监管平台
     */
    public function findAllSupervision($query = array())
    {
        
        $select = new Select(array(
            'o' => $this->getTable()
        ));
        $select->columns(array(
            'catalog_id',
            'org_id',
            'org_name',
            'org_address',
            'org_live',
            'org_logo',
            'org_img1',
            'org_img2',
            'org_img3',
            'org_img4',
            'org_img5',
            'org_img6',
            'org_img7',
            'org_other'
        ));
        
        $select->join(array(
            'c' => 'hy_catalog'
        ), 'c.catalog_id=o.catalog_id', array(
            'catalog_name'
        ), $select::JOIN_LEFT);
        
        $select->order('o.org_id DESC');
        
        return $this->fetchPaginate($select, $query);
    }
    
    /**
     * 查询所有的显示 和 指定类型 的商户
     * @param unknown $type 1 配餐类，2 商城类
     */
    public function findAllDisplay($type, $adminOrgId = '')
    {
        
        $where = array(
            'org_is_display' => 1,
            'org_type' => $type
        );
        
        if($adminOrgId){
            $where['org_id'] = $adminOrgId;
        }
        
        $rowset = $this->select($where);
        
        return $rowset->toArray();
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            $this->getId() => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function findByCatalogId($id)
    {
        $rowset = $this->select(array(
            'catalog_id' => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function create($data)
    {
        $rowset = $this->insert($data);
    
        $id = $this->lastInsertValue;
        $data = array(
            'org_code' => 'hy'.str_pad($id, 5, "0", STR_PAD_LEFT).rand(1000,9999)
        );
        return $this->updateById($data, $id);
    }

    public function updateById($data, $id)
    {
        return $this->update($data, array(
            $this->getId() => $id
        ));
    }

    public function remove($id)
    {
        return $this->delete(array(
            'org_id' => $id
        ));
    }
}